﻿using Engine;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using Game.Server;
using Game.Server.Plugins;
using System.Globalization;
using Game.NetWork;
using LiteNetLib;
using Game.NetWork.Packages;
using System.Threading.Tasks;
using Game.Server.Event;
using GameEntitySystem;

namespace Game
{
    public class CmdBackup : AbstractProcessCmd
    {
        public override string Cmd => "backup";
        public override string Introduce => "/backup m  -存档每隔m分钟自动备份";

        public override int AuthLevel => 1000;

        public override DisplayType Display => DisplayType.Authority;

        public override void ProcessCmd()
        {
            if (GameManager.Project == null)
            {
                SendMessage(Cmd, "请先进入存档");
                return;
            }
            if (m_messageDatas.Length == 1)
            {
                string help = "/backup m   -存档每隔m分钟自动备份，m为-1则不自动备份\n";
                help += "/backup   -备份存档";
                SendMessage(Cmd, help);
                try
                {
                    GameManager.SaveProject(true, false);
                    WorldsManager.MakeQuickWorldBackup();
                    SendMessage(Cmd, "存档备份成功");
                }
                catch (Exception e)
                {
                    SendMessage(Cmd, "存档备份失败: " + e.Message);
                }
            }
            else if(m_messageDatas.Length == 2)
            {
                try
                {
                    float time = (float)double.Parse(m_messageDatas[1]);
                    BackupZHPlugin.BackupTime = time * 60f;
                    if(time > 0)
                    {
                        SendMessage(Cmd, $"设置成功,每{time}分钟存档将自动备份一次");
                    }
                    else
                    {
                        SendMessage(Cmd, $"已关闭存档自动备份");
                    }
                }
                catch
                {
                    SendMessage(Cmd, "输入的指令有误");
                }
            }
            else
            {
                string help = "/backup m   -存档每隔m分钟自动备份，m为-1则不自动备份\n";
                help += "/backup   -备份存档";
                SendMessage(Cmd, help);
            }
        }
    }

    public class BackupZHPlugin : ServerPlugin,ICreatureSpawnEventHandle
    {

        public override string Name => "存档备份插件";

        public override int Version => 10000;

        public byte FirstLevel => 0;

        public static float BackupTime = 20 * 60f;

        public float time;


        public override void Initialize()
        {
            CreatureSpawnEventManager.AddObject(this);
        }

        public override void Load()
        {
            time = 0f;
        }

        public override void Save()
        {
        }

        public bool Update(SubsystemCreatureSpawn subsystemCreatureSpawn, float dt)
        {
            if(BackupTime > 0)
            {
                time += dt;
                if (time > BackupTime)
                {
                    time = 0f;
                    try
                    {
                        WorldsManager.MakeQuickWorldBackup();
                        Log.Information("存档自动备份成功");
                    }
                    catch (Exception e)
                    {
                        Log.Information("存档自动备份失败：" + e.Message);
                    }
                }
            }
            return true;
        }

        public void OnEntityAdded(SubsystemCreatureSpawn subsystemCreatureSpawn, Entity entity)
        {
        }

        public void OnEntityRemoved(SubsystemCreatureSpawn subsystemCreatureSpawn, Entity entity)
        {
        }

        public void OnPlayerSpawned(PlayerData playerData, Entity playerEntity, Vector3 position)
        {
        }

        public void InitCreatureTypes(SubsystemCreatureSpawn subsystemCreatureSpawn, List<SubsystemCreatureSpawn.CreatureType> creatureTypes)
        {
        }
    }
}